package com.ledim.share;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import com.letv.android.young.client.R;
import com.ledim.utils.DensityUtil;
import com.ledim.utils.LedimNetWorkUtils;
import com.ledim.utils.ToastUtil;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class ShareDialogFragment extends DialogFragment
        implements android.view.View.OnClickListener {

    public static final String SHARE_IMG_URL = "share_img_url";
    public static final String SHARE_IMG_PATH = "share_img_path";
    public static final String SHARE_TITLE = "share_title";
    public static final String SHARE_CONTENT = "share_content";
    private static final String SHARE_URL = "share_url";
    private String shareImgUrl;
    private String shareTitle;
    private String shareContent;
    private String shareUrl;
    private String shareImagePath;

    public static ShareDialogFragment getInstance(String imageUrl, String shareContet,
                                                  String shareTitle, String shareUrl) {
        ShareDialogFragment dialog = new ShareDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString(SHARE_IMG_URL, imageUrl);
        bundle.putString(SHARE_CONTENT, shareContet);
        bundle.putString(SHARE_TITLE, shareTitle);
        bundle.putString(SHARE_URL, shareUrl);
        dialog.setArguments(bundle);
        return dialog;
    }

    public static ShareDialogFragment getInstance(String imageUrl, String shareContet,
                                                  String shareTitle, String shareUrl,
                                                  String shareImagePath) {
        ShareDialogFragment dialog = getInstance(imageUrl, shareContet, shareTitle, shareUrl);
        dialog.getArguments().putString(SHARE_IMG_PATH, shareImagePath);
        return dialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.dialog_bottom);
        Bundle bundle = getArguments();
        if (bundle != null) {
            shareImgUrl = bundle.getString(SHARE_IMG_URL);
            shareContent = bundle.getString(SHARE_CONTENT);
            shareTitle = bundle.getString(SHARE_TITLE);
            shareUrl = bundle.getString(SHARE_URL);
            shareImagePath = bundle.getString(SHARE_IMG_PATH);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().setCanceledOnTouchOutside(true);
        View view = inflater.inflate(R.layout.share_dialog, container, false);
        view.findViewById(R.id.timeline_share).setOnClickListener(this);
        view.findViewById(R.id.weixin_share).setOnClickListener(this);
        view.findViewById(R.id.weibo_share).setOnClickListener(this);
        view.findViewById(R.id.qqfriend_share).setOnClickListener(this);
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.height = DensityUtil.dip2px(getActivity(), 143);
        return view;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void onClick(View v) {
        if (!LedimNetWorkUtils.isNetworkAvailable(getActivity())) {
            ToastUtil.toast(R.string.net_error);
            return;
        }
        switch (v.getId()) {
            case R.id.timeline_share:
                LedimShareUtil.shareWebpageToWechat(shareUrl, "", "", shareImagePath, true);
                break;
            case R.id.weixin_share:
                LedimShareUtil.shareWebpageToWechat(shareUrl, getResources().getString(R.string.weixin_share_title), getWebChatShareContent(shareTitle), shareImagePath, false);
                break;
            case R.id.weibo_share:
                LedimShareUtil.shareToWeiBo(getActivity(), getWeiboShareContent(shareTitle, shareUrl), shareImagePath);
                break;
            case R.id.qqfriend_share:
                LedimShareUtil.sharToQZONE(getActivity(), getResources().getString(R.string.weixin_share_title), getWebChatShareContent(shareTitle), shareUrl, shareImgUrl);
                break;

        }
    }

    /**
     * @param shareTitle
     * @param shareUrl
     * @return 获取新浪微博的分享文案
     */
    private String getWeiboShareContent(String shareTitle, String shareUrl) {
        String format =
                String.format(getResources().getString(R.string.sina_share_des), shareTitle);
        return format + shareUrl;
    }

    /**
     * @param shareTitle
     * @return 获取微信好友的分享文案
     */
    private String getWebChatShareContent(String shareTitle) {
        return String.format(getResources().getString(R.string.weixin_share_des), shareTitle);

    }
}
